/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tractorlk.drops;

import java.util.Comparator;
import java.lang.Math;

/**
 *
 * @author Kevin
 */
public class CardComparator implements Comparator
{
    
    int rank;
    int suit;
    
    public CardComparator()
    {
        this.rank = -1;
        this.suit = -1;
    }

    public CardComparator(int rank, int suit)
    {
        this.rank = rank;
        this.suit = suit;
    }

    /* AS:51 2T:52...skip trump rank ...AT:63 R:64 TR:65 BJ:66 RJ:67 */
    public int compare(Object a1, Object b1)
    {
        Card a = (Card)a1;
        Card b = (Card)b1;  
        int aID = a.getGameID();
        if( aID == -1)
        {
            aID = a.getID()%54;
            if(a.getRank()== this.rank) //rank
            {
                aID = 64;
                if(a.getSuit() == this.suit)    //trump rank
                {
                    aID = 65;
                }
            }
            else if (aID == 52) //black joker
                aID = 66;
            else if(aID == 53)  //red joker
                aID = 67;
            else if (a.getSuit() == this.suit)
            {
                if(a.getRank() < this.rank)
                    aID = aID%13+52;
                else
                    aID = aID%13+51;
            }
            a.setGameID(aID);
         }
        
        int bID = b.getGameID();
        if (bID == -1)
        {
            bID = b.getID()%54;  
            if(b.getRank()== this.rank)
            {
                bID = 64;
                if(b.getSuit() == this.suit)
                {
                    bID = 65;
                }
            }
            else if (bID == 52)
                bID = 66;
            else if(bID == 53)
                bID = 67;
            else if (b.getSuit() == this.suit)
            {
                if(b.getRank() < this.rank)
                    bID = bID%13+52;
                else
                    bID = bID%13+51;
            }
            b.setGameID(bID);
        }
        //case for equal trump rank
        if(aID-bID == 0)
        {
            aID = aID + a.getSuit();
            bID = bID + b.getSuit();
        }

        return (aID-bID);

        //throw new UnsupportedOperationException("Not supported yet.");
    }


    /* Compares for showing hand  */
    public boolean winner(Card led1, Hand played1, int handType, Hand highCards)
    {
        Card led = (Card)led1;

        if(handType(played1, highCards) != handType)
            return false;

        //if didnt follow type (double tractor single)
        if(handType < 0 && played1.hasDoubles(suit, this.suit, this.rank) == Math.abs(handType)%100 && suitType(played1) == this.suit)
        {
            ; //copied number of doubles on trump rank
        }
        
        Card played = played1.getMaxCard();
        
        int ledID = led.getID()%54; //dummy value in case leading first card is always winner

        int ledGameID = led.getGameID();
        int playedGameID = played.getGameID();

        int ledSuit = getSuit(led); //suit (5 if trump suit/rank/joker)
        int playedSuit = getSuit(played);

        if(ledID < 0)       //new lead
        {
            System.out.println("**********Led card: Auto win");
            return false;
        }
        else if(ledSuit == playedSuit && playedGameID > ledGameID) //follow suit and played higher
        {
            System.out.println("**********Follow suit and played higher");
            return false;
        }
        else if(ledSuit != playedSuit && playedSuit == 5) //got trumped
        {
            System.out.println("**********Winner isnt trump, and got trumped");
            return false;
        }
        else
            return true;
    }

    public int getSuit(Card c)
    {
        if(c.getSuit() == this.suit)
            return 5;
        else if(c.getRank() == this.rank)
            return 5;
        else
            return c.getSuit();
    }

    public int points(Hand a, Hand b, Hand c, Hand d)
    {
        int total = 0;
        total = total + a.points();
        total = total + b.points();
        total = total + c.points();
        total = total + d.points();
        return total;
    }

    public int points(Hand a)
    {
        int total = 0;
        total = total + a.points();
        return total;
    }

    public boolean hasSuit(Hand a, int suit)
    {
        if(suit == this.suit || suit == 5)
            return (a.hasSuit(suit, this.rank, this.suit) || a.hasSuit(this.suit, this.rank, this.suit) || a.hasSuit(5, this.rank, this.suit));
        else
            return a.hasSuit(suit, this.rank, this.suit);
    }

    public int hasSuit(Hand a, int suit, int dummy)
    {
        if(suit == 5)
            return  a.hasSuit(this.suit, this.rank, this.suit, dummy);
        else
            return a.hasSuit(suit, this.rank, this.suit, dummy);
    }

    public int handType(Hand a, Hand highCards)
    {
        int tractor = a.tractorSize(this.suit, this.rank);
        int highCardNum = 0;
        if(suitType(a) != 0)
            highCardNum= a.highCardCount(highCards, this.suit, this.rank);
        if(a.isSingle())
            return 1;
        else if(tractor >= 4)
            return tractor;
        else if (a.isDouble())
            return 2;
        else if (highCardNum < -1)
            return highCardNum;
        else 
            return 0;
    }

    public int check(Hand main, Hand play, int type, int suit, Hand highCards)
    {
       // System.out.println("************************");
        System.out.println("Type: " + type);
       // System.out.println("handType(play, highCards) " + handType(play, highCards));
       // System.out.println("main.hasDoubles(suit, this.suit, this.rank) " + main.hasDoubles(suit, this.suit, this.rank));
       // System.out.println("Type: " + type);
       // System.out.println("Type: " + type);
        //System.out.println("************************");
        if (type == 0)  
        { //if first card led
            System.out.println("type == 0, returned 1");
            return 1;
        }
        if(play.getSize() != Math.abs(type%100))
        { //if didnt play the right number of cards
            System.out.println("Did not click correct number of cards");
            return -1;
        }
        if(hasSuit(play, suit, 0) < Math.abs(type%100) && hasSuit(main, suit, 0) > hasSuit(play, suit, 0) )
        { //if didnt follow suit
            System.out.println("Did not follow suit, return -1");
            return -1;
        }
        if (handType(play, highCards) == type)
        { //if played the correct type
            System.out.println("Played the correct handType, return 1");
            return 1;
        }
        if(type == 2 && main.hasDoubles(suit, this.suit, this.rank) > 0)
        { //if doubles and had doubles but didnt play
            System.out.println("Did not play 1 double of suit, return -1");
            return -1;
        }
        if(type >= 4 && play.tractorSize(this.suit, this.rank) == type)
        { //if tractor and the appropriate tractor
            System.out.println("Played the correct tractor, return 1");
            return 1;
        }
        if(type >= 4 && play.hasDoubles(suit, this.suit, this.rank) == type/2)
        { //if tractor and played the correct number of doubles
            System.out.println("For a tractor, played the correct number of doubles (not a tractor though), return 0");
            return 0;
        }
        if(type >=4 && main.hasDoubles(suit, this.suit, this.rank) > play.hasDoubles(suit, this.suit, this.rank))
        { //if tractor and didnt play the correct number of doubles
            System.out.println("For a tractor, did not play the correct number of doubles, return -1");
            return -1;
        }
        if(type < 0 && play.hasDoubles(this.suit, this.suit, this.rank) == type/-100 && play.getHand().size() == Math.abs(type%100) && suitType(play) == this.suit)
        { //if set of high cards and played the correct number of suit and trump suit
            System.out.println("for highcard, played doubles and all trump, return 1");
            return 1;
        }
        if(type < 0 && play.hasDoubles(suit, this.suit, this.rank) < type/-100 && main.hasDoubles(suit, this.suit, this.rank) > play.hasDoubles(suit, this.suit, this.rank) )
        { //if didnt play the correct number of doubles for high cards
            System.out.println("For high cards, did not play the correct number of doubles, return -1");
            return -1;
        }
        if(play.getSize() == Math.abs(type%100))
        { //if played the correct number of cards
            System.out.println("played the same number of cards, return 0");
            return 0;
        }
        System.out.println("Reached end (wrong number of cards played) return -1");
        return -1;

    }

    public int suitType(Hand play)
    {
        int suitType = 0;
        if(hasSuit(play, 1))
            suitType = suitType*6+1;
        if(hasSuit(play, 2))
            suitType = suitType*6+2;
        if(hasSuit(play,3))
            suitType = suitType*6+3;
        if(hasSuit(play, 4))
            suitType = suitType*6+4;
        if(suitType > 5)
            suitType = 0;
        System.out.println("Suit type " + suitType);
        return suitType;
    }

    public boolean isLead(Hand play, Hand highCards)
    {
        if(handType(play, highCards) != 0)
            return true;
        return false;
    }


}
